package com.AdvancedDemos.FileAndIO.练习5;

import java.io.*;
import java.util.ArrayList;
import java.util.Properties;

public class 练习5 {
    public static final Properties properties = new Properties();

    public static void main(String[] args) throws IOException {
        /*
        批阅选择题
	    给学生提供答题卡:
	        要求文件命名是学生姓名.txt
	        文件内部
	            1-5题:单选
	                书写格式:
	                    1,答案
	                    2,答案
	                    ...
	            6-10题:多选
	                    6,答案 例如 6,AB 或者 6,ab
	            单选题一题2分
	            多选题一题5分 -- 错选漏选没分
	    配置文件信息中.properties,书写的是正确答案
	    1=A
	    2=B
	    ...
	    6=ABC
		...
	    根据标准答案,和学生的答案,计算学生选择题分数
	    将学生的姓名和分数组成一对,写入到scores.txt文件中
	    */

        //从answer.properties中读取正确答案并存储
        getAnswer();
        System.out.println(properties);//{1=A, 2=B, 3=C, 4=D, 5=A, 6=AB, 7=AC, 8=AD, 9=ABC, 10=ABCD}

        //读取学生答题卡
        File file = new File("helloworld\\src\\com\\AdvancedDemos\\FileAndIO\\练习5\\答题卡");
        File[] files = file.listFiles();//获取路径包含的文件路径/文件夹路径
        ArrayList<Student> students = new ArrayList<>();
        for (File file1 : files) {
            Student student = new Student();
            String name = file1.getName().split("\\.")[0];//获取路径表示的文件/文件夹名
            System.out.println(name);
            //PDD
            //卢本伟
            //大司马
            student.setName(name);
            //读取学生答题卡文件内容,并计算成绩
            //方法返回学生的成绩,参数接收学生答题卡的文件路径
            int score = getScore(file1);
            student.setScore(score);
            students.add(student);
        }
        //写出成绩
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("helloworld\\src\\com\\AdvancedDemos\\FileAndIO\\练习5\\scores.txt"));
        for (Student student : students) {
            bufferedWriter.write(student.toString());
            bufferedWriter.newLine();
        }
        bufferedWriter.close();
        System.out.println("写出完成!");
    }

    private static int getScore(File file1) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file1));
        String line;
        int score = 0;
        while ((line = br.readLine()) != null) {
            String[] split = line.split("\\.");
            String number = split[0];
            String stuAnswer = split[1];
            //计算成绩
            int intNumber = Integer.parseInt(number);
            //匹配答案
            if (properties.getProperty(number).equalsIgnoreCase(stuAnswer)) {
                if (intNumber >= 1 && intNumber <= 5) {//单选
                    score += 2;
                } else {//多选
                    score += 5;
                }
            }
        }
        return score;
    }

    private static void getAnswer() throws IOException {
        FileReader fileReader = new FileReader("helloworld\\src\\com\\AdvancedDemos\\FileAndIO\\练习5\\answer.properties");
        properties.load(fileReader);
        fileReader.close();
    }
}


class Student {
    private String name;
    private int score;

    public Student() {
    }

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return name + "=" + score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}
